package com.huluobo.a2201mvvm_imdemo.shop;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.huluobo.a2201mvvm_imdemo.R;
import com.huluobo.a2201mvvm_imdemo.databinding.ActivityDetailBinding;
import com.huluobo.a2201mvvm_imdemo.mvvm.BaseMVVMActivity;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;

public class DetailActivity extends BaseMVVMActivity<ActivityDetailBinding, DetailViewModel> {

    @Override
    protected void initData() {
        int goodsId = getIntent().getIntExtra("goodsId", 0);
        viewModel.getGoodsDetail(goodsId);

        viewModel.success.observe(this, goodsDetailBeanResult -> {
            if (goodsDetailBeanResult.getCode() == 200) {
                dataBinding.detailBanner.setImages(goodsDetailBeanResult.getData().getBannerList());
                dataBinding.detailBanner.start();
                dataBinding.detailDesc.setText(goodsDetailBeanResult.getData().getGoods_desc());
                dataBinding.detailPrice.setText("价格:￥" + goodsDetailBeanResult.getData().getGoods_default_price() +
                        "元");
                dataBinding.detailCode.setText("商品编号:" + goodsDetailBeanResult.getData().getGoods_code());
                Glide.with(this).load(goodsDetailBeanResult.getData().getGoods_detail_one()).into(dataBinding.detailImg1);
                Glide.with(this).load(goodsDetailBeanResult.getData().getGoods_detail_two()).into(dataBinding.detailImg2);

            } else {
                ToastUtils.showLong(goodsDetailBeanResult.getMessage());
            }
        });

        viewModel.failed.observe(this, s -> {
            Log.i("TagA", "商品详情失败:" + s);
        });
    }

    @Override
    protected void initView() {
        //初始化banner
        dataBinding.detailBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);//指示器风格
        dataBinding.detailBanner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                Glide.with(context).load(path).into(imageView);
            }
        });//banner设置图片加载器
        dataBinding.detailBanner.setDelayTime(1500);//设置翻页时间
        dataBinding.detailBanner.setIndicatorGravity(BannerConfig.CENTER);
    }

    @Override
    protected Class<DetailViewModel> getViewModel() {
        return DetailViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_detail;
    }
}